package com.iwhalecloud.bss.kite.cucc.service.custview.evolve.task;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.inf.IInfServiceBus;
import com.iwhalecloud.bss.kite.cucc.client.dto.order.ProdInstQryDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.order.UserQueryDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosGoodsDTO;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.CustViewAttr;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.CustViewBaseVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.CustViewVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.GroupUserInfoVO;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Offer;

import com.ztesoft.bss.common.util.DateUtil;
import com.ztesoft.bss.common.util.SpringUtil;

/**
 * @ClassName : MemberUserInfoTask
 * @Description : 成员用户信息
 * @Author : xie.zhengjun
 * @Date : 2021/5/8 16:43
 */
public class MemberUserInfoTask extends AbstractCustViewTask{
    @Override
    public CustViewBaseVO call(Object[] args) {
        Map<String, Object> userDetail = (Map<String, Object>) args[0];
        CustViewVO custViewVO = (CustViewVO) args[1];
        KitePosDTO kitePosDTO = (KitePosDTO) args[2];

        GroupUserInfoVO groupUserInfoVO = new GroupUserInfoVO();
        custViewVO.setGroupUser(groupUserInfoVO);
        if (KiteMapUtils.isNotEmpty(userDetail)) {
            // 用户信息
            Map<String, Object> userInfo = (Map<String, Object>) userDetail.get("USER_INFO");
            // 合同信息
            List<Map<String, Object>> userContractInfos = (List<Map<String, Object>>) userDetail
                .get("USER_CONTRACT_INFOS");
            // 发展人信息
            List<Map<String, Object>> userDevelopInfos = (List<Map<String, Object>>) userDetail
                .get("USER_DEVELOP_INFOS");
            assembleGroupUserInfoVO(groupUserInfoVO, userInfo, userContractInfos, kitePosDTO, userDevelopInfos);
        }
        return groupUserInfoVO;
    }

    private void assembleGroupUserInfoVO(GroupUserInfoVO groupUserInfoVO, Map<String, Object> userInfo,
        List<Map<String, Object>> userContractInfos, KitePosDTO kitePosDTO,
        List<Map<String, Object>> userDevelopInfos) {
        String custId = KiteMapUtils.getString(userInfo, "CUST_ID");

        // 1 成员用户号码 SERIAL_NUMBER
        String serialNumberVal = KiteMapUtils.getString(userInfo, "SERIAL_NUMBER");
        CustViewAttr serialNumber = new CustViewAttr("serialNumber", "成员用户号码", serialNumberVal, serialNumberVal);

        // 2 成员用户名称 USER_NAME
        String userNameVal = KiteMapUtils.getString(userInfo, "USER_NAME");
        CustViewAttr userName = new CustViewAttr("userName", "成员用户名称", userNameVal, userNameVal);

        // 3 成员归属地 PROVINCE_CODE EPARCHY_CODE CITY_CODE
        String provinceCode = KiteMapUtils.getString(userInfo, "PROVINCE_CODE");
        String eparchyCode = KiteMapUtils.getString(userInfo, "EPARCHY_CODE");
        String cityCode = KiteMapUtils.getString(userInfo, "CITY_CODE");
        String regionValDesc = String.join(SymbolConsts.HYPHEN, LocalAttrHelper.getRegionName(provinceCode),
            LocalAttrHelper.getRegionName(eparchyCode), LocalAttrHelper.getRegionName(cityCode));
        String regionVal = String.join(SymbolConsts.HYPHEN, provinceCode, eparchyCode, cityCode);
        CustViewAttr region = new CustViewAttr("region", "成员归属地", regionVal, regionValDesc);

        // 4 成员产品名称 GOODS_ID
        String goodsId = KiteMapUtils.getString(userInfo, "GOODS_ID");
        String prodNameDesc = Optional.ofNullable(OfferCache.get(goodsId)).map(Offer::getOfferName).orElse(goodsId);
        CustViewAttr prodName = new CustViewAttr("prodName", "成员产品名称", goodsId, prodNameDesc);

        // 5 成员客户证件

        //6 成员证件号码

        // 7 成员用户状态
        String userStateCode = Optional.ofNullable(String.valueOf(userInfo.get("SERVICE_STATE_CODE"))).orElse("");
        String userStateCodeDesc = Optional.ofNullable(AttrValueCache.getAttrValue("2020012105", userStateCode))
            .map(AttrValue::getAttrValueName).orElse(userStateCode);
        CustViewAttr state = new CustViewAttr("state", "成员用户状态", userStateCode, userStateCodeDesc);

        // 8 成员加入时间
        String startDate = "";
        String endDate = "";
        if (!KiteObjectUtils.isEmpty(kitePosDTO) && KiteListUtils.isNotEmpty(kitePosDTO.getGoods())) {
            List<KitePosGoodsDTO> kitePosGoodsDTOList = kitePosDTO.getGoods();
            KitePosGoodsDTO kitePosGoods = kitePosGoodsDTOList.get(0);
            startDate = kitePosGoods.getStartDate();
            endDate = kitePosGoods.getEndDate();
        }
        Date dStartDate = DateUtil.strToDate(startDate);
        startDate = DateUtil.format(dStartDate);
        CustViewAttr startDateAttr = new CustViewAttr("state", "成员加入时间", startDate, startDate);

        // 9 成员退出时间
        Date dEndDate = DateUtil.strToDate(endDate);
        endDate = DateUtil.format(dEndDate);
        CustViewAttr endDateAttr = new CustViewAttr("endDate", "成员退出时间", endDate, endDate);

        // 10 开户员工
        ProdInstQryDTO prodInstQryDTO = getProdInstQryDTO(custId, serialNumberVal, provinceCode, eparchyCode);
        CustViewAttr openStaffName = new CustViewAttr("openStaffId", "开户员工", prodInstQryDTO.getOpen_staff_id(),
            prodInstQryDTO.getOpen_staff_name());

        // 11 开户时间 OPEN_DATE
        String openDateVal = KiteMapUtils.getString(userInfo, "OPEN_DATE");
        Date dOpenDateVal = DateUtil.strToDate(openDateVal);
        openDateVal = DateUtil.format(dOpenDateVal);
        CustViewAttr openDate = new CustViewAttr("openDate", "开户时间", openDateVal, openDateVal);

        // 12 成员用户合同 USER_CONTRACT_INFOS[0].CONTRACT_ID
        String contractId = "";
        if (KiteListUtils.isNotEmpty(userContractInfos)) {
            Map<String, Object> userContractInfo = userContractInfos.get(0);
            contractId = KiteMapUtils.getString(userContractInfo, "CONTRACT_ID");
        }
        CustViewAttr contract = new CustViewAttr("contract", "成员用户合同", contractId, "详情");

        // 13 发展人
        String developStaffName = "";
        String channelName = "";
        if (KiteListUtils.isNotEmpty(userDevelopInfos)) {
            Map<String, Object> userDevelopInfo = userDevelopInfos.get(0);
            developStaffName = KiteMapUtils.getString(userDevelopInfo, "DEVELOP_STAFF_NAME");
            String developDepartId = KiteMapUtils.getString(userDevelopInfo, "DEVELOP_DEPART_ID");
            IInfServiceBus infServiceBus = SpringUtil.getBean(IInfServiceBus.class);
            channelName = infServiceBus.queryDeveloChannelName(developDepartId, provinceCode);
            channelName = Optional.ofNullable(channelName).orElse("");
        }
        CustViewAttr developerName = new CustViewAttr("发展人", developStaffName, developStaffName);

        // 14 发展渠道
        CustViewAttr developerChannelName = new CustViewAttr("发展渠道", channelName, channelName);
        groupUserInfoVO.setContractId(contractId);
        groupUserInfoVO.setCustId(custId);
        groupUserInfoVO.setProvinceCode(provinceCode);
        groupUserInfoVO.setAttrs(Lists
            .newArrayList(serialNumber, userName, region, prodName, state, startDateAttr, endDateAttr, openStaffName,
                openDate, contract, developerName, developerChannelName));
    }

    /**
     * 查询某个用户的用户列表信息
     *
     * @param custId
     * @param serialNumberVal
     * @param provinceCode
     * @param eparchyCode
     * @return
     */
    private ProdInstQryDTO getProdInstQryDTO(String custId, String serialNumberVal, String provinceCode,
        String eparchyCode) {
        IInfServiceBus infServiceBus = SpringUtil.getBean(IInfServiceBus.class);
        ProdInstQryDTO prodInstQryDTO = null;
        Map<String, String> map = new HashMap<>(5);
        map.put("custId", custId);
        map.put("accNum", serialNumberVal);
        map.put("eparchyCode", eparchyCode);
        map.put("provinceCode", provinceCode);
        UserQueryDTO userQueryDTO = infServiceBus.queryUserList(map);
        if (Objects.nonNull(userQueryDTO)) {
            List<ProdInstQryDTO> userList = userQueryDTO.getUserList().getList();
            if (KiteListUtils.isNotEmpty(userList)) {
                prodInstQryDTO = userList.get(0);
            }
        }
        return prodInstQryDTO;
    }
}
